<template>
  <div>
    <!--注册-->
    <el-dialog
      :visible.sync="dialogVisible"
      width="24%"
      :before-close="handleClose"
    >
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
        <el-form-item>
          <h2>注册</h2>
        </el-form-item>
        <el-form-item prop="userName">
          <el-input
            v-model="ruleForm.userName"
            placeholder="请输入账号"
            prefix-icon="el-icon-user-solid"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="ruleForm.password"
            placeholder="请输入密码"
            prefix-icon="el-icon-view"
          ></el-input>
        </el-form-item>
        <el-form-item prop="losePassword">
          <el-input
            v-model="ruleForm.losePassword"
            placeholder="请再次输入密码"
            prefix-icon="el-icon-view"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleClo">注册</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import loginRegister from "../api/loginRegister";
export default {
  props: ["dialogVisible"],
  components: {},
  name: "RegisterModule",
  data() {
    var userName = (rule, value, lc) => {
      const regUserName = /^[a-zA-Z]\w{5,16}$/;
      if (regUserName.test(value)) {
        return lc();
      }
      lc(new Error("字母开头,长度5-16之间,允许字母数字下划线"));
    };

    var existsUserName = (rule, value, lc) => {
      loginRegister.getExists({ userName: value }).then((res) => {
        console.log(res);
        if (res.data.code === "001") {
          return lc();
        } else {
          lc(new Error("用户名已经存在，不能注册"));
        }
      });
    };

    var password = (rule, value, lc) => {
      const rulPassword = /^[a-zA-Z]\w{6,18}$/;
      if (rulPassword.test(value)) {
        return lc();
      }
      lc(new Error("字母开头,长度6-18之间,允许字母数字下划线"));
    };

    var losePassword = (rule, value, lc) => {
      if (value === this.ruleForm.password) {
        return lc();
      }
      lc(new Error("两次输入密码不一致"));
    };
    return {
      ruleForm: {
        userName: "",
        password: "",
        losePassword: "",
      },
      rules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            validator: userName,
            message: "字母开头,长度5-16之间,允许字母数字下划线",
            trigger: "blur",
          },
          {
            validator: existsUserName,
            message: "用户名已经存在，不能注册",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            validator: password,
            message: "字母开头,长度6-18之间,允许字母数字下划线",
            trigger: "blur",
          },
        ],
        losePassword: [
          { required: true, message: "请输入确认密码", trigger: "blur" },
          {
            validator: losePassword,
            message: "两次输入密码不一致",
            trigger: "blur",
          },
        ],
      },
    };
  },

  created() {},
  methods: {
    handleClose(done) {
      this.$emit("register");
    },
    async handleClo() {
      let { data: res } = await loginRegister.getRegister({
        userName: this.ruleForm.userName,
        password: this.ruleForm.password,
      });
      console.log(res);
      this.$notify({
        title: "注册成功",
        message: "恭喜您注册成功，快快登陆去吧",
        type: "success",
      });
      this.ruleForm = "";
      this.$emit("flagTwo");
    },
  },
  computed: {},
  watch: {},
};
</script>

<style lang="scss" scoped>
.el-dialog {
  .el-button {
    width: 100%;
  }
  h2 {
    text-align: center;
  }
}
</style>